﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using mbine.msgc.model;
using mbine.msgc.tool;
using mbine.msgc.controls;
using System.Threading;

namespace mbine.msgc.controls
{
    /// <summary>
    /// Interaction logic for UCChatList.xaml
    /// </summary>
    public partial class UCChatList : UserControl, IObserveRoster
    {
        public event EventHandler EventThreadChanged;
        public event EventHandler EventSetThread;
        public CThread NowThread { get; set; }

        public UCChatList()
        {
            InitializeComponent();
        }

        public void Add(List<IUser> _users, string _key)
        {
            bool IsExist = false;
            foreach (CThread thread in CCUser.Instance.ThreadList)
            {
                if (thread.Key == _key)
                {
                    IsExist = true;
                    thread.Add(_users);
                    NowThread = thread;
                }
            }
            if (!IsExist)
            {
                Dispatcher.Invoke(new ThreadStart(() =>
                {
                    UCThread thrd = new UCThread();
                    if (_key != "")
                        thrd.Thread.Key = _key; 
                    thrd.Thread.Members = _users;
                    thrd.SetMembers();
                    ListThread.Items.Add(thrd);
                    NowThread = thrd.Thread;

                    ListThread.SelectedItem = thrd;
                }));
            }

            if (EventSetThread != null)
                EventSetThread(this, null);
        }

        public void Update(CThread _thread)
        {
        }

        public void Update()
        {
        }

        public void UpdateRoster()
        {
        }

        private void ListThread_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ListThread.SelectedItem != null)
            {
                NowThread = ((UCThread)ListThread.SelectedItem).Thread;
                if (EventThreadChanged != null)
                    EventThreadChanged(this, null);
            }
        }

        private void ListThread_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("mbine.msgc.model.CUser"))
            {
                CUser contact = e.Data.GetData("mbine.msgc.model.CUser") as CUser;
                UCThread trd = (UCThread)((ListViewItem)sender).Content;

                Add(new List<IUser> { contact }, trd.Thread.Key);
                trd.SetMembers();
            }
        }

        private void ListThread_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }
    }
}
